﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ewk.Extensions.UnitTests.UriExtensionsTests
{
    [TestClass]
    public class AddQueryStringParametersTests
    {
        [TestMethod]
        public void AddQueryStringParameters_adds_queryString_parameters_to_the_given_uri()
        {
            const string baseUrl = "http://some.url";
            const string addedQueryString = "key1=value1&key2=value2&key3=value3";
            var expectedResult = string.Format("{0}/?{1}", baseUrl, addedQueryString);

            IDictionary<string, string> parameters = new Dictionary<string, string>
                {
                    {"key1", "value1"},
                    {"key2", "value2"},
                    {"key3", "value3"},
                };

            var uri = new Uri(baseUrl);
            var result = uri.AddQueryStringParameters(parameters);

            Assert.AreEqual(expectedResult, result.AbsoluteUri);
        }

        [TestMethod]
        public void AddQueryStringParameters_adds_but_does_not_override_queryString_parameters_to_the_given_uri()
        {
            const string baseUrl = "http://some.url";
            const string originalQuerystring = "key2=originalValue2";
            const string addedQueryString = "key1=value1&key3=value3";

            var originalUrl = string.Format("{0}?{1}", baseUrl, originalQuerystring);
            var expectedResult = string.Format("{0}/?{1}&{2}", baseUrl, originalQuerystring, addedQueryString);

            IDictionary<string, string> parameters = new Dictionary<string, string>
                {
                    {"key1", "value1"},
                    {"key2", "value2"},
                    {"key3", "value3"},
                };

            var uri = new Uri(originalUrl);
            var result = uri.AddQueryStringParameters(parameters);

            Assert.AreEqual(expectedResult, result.AbsoluteUri);
        }

        [TestMethod]
        public void AddQueryStringParameters_adds_queryString_parameters_to_the_given_url()
        {
            const string baseUrl = "http://some.url";
            const string addedQueryString = "key1=value1&key2=value2&key3=value3";
            var expectedResult = string.Format("{0}/?{1}", baseUrl, addedQueryString);

            IDictionary<string, string> parameters = new Dictionary<string, string>
                {
                    {"key1", "value1"},
                    {"key2", "value2"},
                    {"key3", "value3"},
                };

            var result = baseUrl.AddQueryStringParameters(parameters);

            Assert.AreEqual(expectedResult, result.AbsoluteUri);
        }

        [TestMethod]
        public void AddQueryStringParameters_adds_but_does_not_override_queryString_parameters_to_the_given_url()
        {
            const string baseUrl = "http://some.url";
            const string originalQuerystring = "key2=originalValue2";
            const string addedQueryString = "key1=value1&key3=value3";

            var originalUrl = string.Format("{0}?{1}", baseUrl, originalQuerystring);
            var expectedResult = string.Format("{0}/?{1}&{2}", baseUrl, originalQuerystring, addedQueryString);

            IDictionary<string, string> parameters = new Dictionary<string, string>
                {
                    {"key1", "value1"},
                    {"key2", "value2"},
                    {"key3", "value3"},
                };

            var result = originalUrl.AddQueryStringParameters(parameters);

            Assert.AreEqual(expectedResult, result.AbsoluteUri);
        }

        [TestMethod]
        public void AddQueryStringParameters_does_not_break_when_empty_querystring_parameters_are_added_to_A_url_with_a_queryString()
        {
            const string baseUrl = "http://some.url";
            const string originalQuerystring = "key2=originalValue2";

            var originalUrl = string.Format("{0}?{1}", baseUrl, originalQuerystring);
            var expectedResult = string.Format("{0}/?{1}", baseUrl, originalQuerystring);

            IDictionary<string, string> parameters = new Dictionary<string, string>();

            var result = originalUrl.AddQueryStringParameters(parameters);

            Assert.AreEqual(expectedResult, result.AbsoluteUri);
        }

        [TestMethod]
        public void AddQueryStringParameters_does_not_break_when_empty_querystring_parameters_are_added()
        {
            const string baseUrl = "http://some.url";

            IDictionary<string, string> parameters = new Dictionary<string, string>();

            var result = baseUrl.AddQueryStringParameters(parameters);

            Assert.AreEqual(baseUrl + "/", result.AbsoluteUri);
        }
    }
}